package megrez.oss.minio.utils;

import java.util.HashMap;
import java.util.Map;

import org.springframework.util.StringUtils;

import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * 用户元数据
 *  
 * @author Lucky Yang
 * @since 2.6.9
 */
@Data
@AllArgsConstructor
public class UserData {
    public static final String FIELD_FILE_NAME = "filename";
    public static final String FIELD_CREATED_BY = "createdby";
    public static final String FIELD_OBJECT_ID = "objectid";

    private String fileName;
    private String createdBy;
    private String objectId;  
    
    public Map<String, String> toMap() {
        if (! (StringUtils.hasText(fileName) && StringUtils.hasText(createdBy) && StringUtils.hasText(objectId))) {
            throw new IllegalArgumentException("User metadata must have value");
        }
        final Map<String, String> rslt = new HashMap<>();
        rslt.put(FIELD_FILE_NAME, this.fileName);
        rslt.put(FIELD_CREATED_BY, this.createdBy);
        rslt.put(FIELD_OBJECT_ID, this.objectId);
        return rslt;
    }  
    
    public static UserData of(Map<String, String> map) {
        return new UserData(map.get(FIELD_FILE_NAME), map.get(FIELD_CREATED_BY), map.get(FIELD_OBJECT_ID));
    }
}
